<template>
  <view class="food">
    <image class="foodImage" :src="props.src"></image>
    <view class="foodContent">
      <view class="foodTitle">{{ props.title }}</view>
      <view class="foodDetail">{{ props.detail }}</view>
      <view class="foodAdd">
        <view @click="add(-1)" :class="['decrease', count > 0 ? 'numActive' : '']">-</view>
        <view :class="['num', count > 0 ? 'numActive' : '']">{{ count }}</view>
        <view @click="add(1)" :class="['increase', count > 0 ? 'increaseActive' : '']" class="increase">+</view>
      </view>
      <view class="price">￥{{ props.singlePrice }}</view>
    </view>
  </view>
</template>

<script setup lang="ts">
import Config from "@/config";
import Store from "@/store";
import { defineProps, onMounted, onUnmounted, ref } from "vue";
const props = defineProps({
  idx: {
    type: Number,
    default: 0,
  },
  src: {
    type: String,
    default: 0,
  },
  title: {
    type: String,
    default: 0,
  },
  detail: {
    type: String,
    default: 0,
  },
  singlePrice: {
    type: Number,
    default: 0,
  },
});
onMounted(() => {
  count.value = Store.Common.getShopCar()[props.idx]?.count || 0;
  uni.$on(Config.Event.setShopCar + props.idx, () => {
    count.value = Store.Common.getShopCar()[props.idx]?.count || 0;
  });
});
onUnmounted(() => {
  uni.$off(Config.Event.setShopCar + props.idx);
});
const count = ref(Store.Common.getShopCar()[props.idx]?.count || 0);
const add = (value: number) => {
  count.value = count.value + value;
  Store.Common.setShopCar(props.idx, {
    src: props.src,
    title: props.title,
    singlePrice: props.singlePrice,
    detail: props.detail,
    id: props.idx,
    count: count.value,
  });
  console.log(Store.Common.getShopCar());
};
</script>
<style scoped>
.food {
  width: 530rpx;
  /* padding-bottom: 20rpx; */
  height: 200rpx;
  display: flex;
  align-items: center;
  justify-content: space-around;
  background-color: white;
  /* box-shadow: 0 0 10rpx rgba(0, 0, 0, 0.1); */
}
.foodImage {
  height: 180rpx;
  width: 180rpx;
  margin: 10rpx;
}
.foodContent {
  width: 310rpx;
  margin: 10rpx;
  height: 180rpx;
  position: relative;
}
.foodAdd {
  transform: translateY(0%);
  position: absolute;
  right: 0;
  bottom: 0;
  display: flex;
}
.increase {
  height: 45rpx;
  width: 45rpx;
  line-height: 45rpx;
  text-align: center;
  font-weight: bold;
  font-size: 30rpx;
  background-color: #fff;
  color: black;
  transform: rotateZ(0deg);
  transition: all 0.3s;
  box-shadow: 0 0 5rpx rgba(0, 0, 0, 1) inset;
  border-radius: 50%;
  vertical-align: middle;
}
.decrease {
  height: 45rpx;
  width: 45rpx;
  line-height: 45rpx;
  text-align: center;
  font-weight: bold;
  font-size: 30rpx;
  background-color: #fff;
  color: black;
  opacity: 0;
  transform: translateX(200%);
  transition: all 0.5s;
  box-shadow: 0 0 5rpx rgba(0, 0, 0, 1) inset;
  border-radius: 50%;
  vertical-align: middle;
}
.num {
  height: 45rpx;
  width: 45rpx;
  line-height: 45rpx;
  text-align: center;
  font-size: 30rpx;
  background-color: white;
  color: black;
  transform: translateX(100%);
  visibility: hidden;
  transition: all 0.5s;
  margin: 0 1rpx;
}
.price {
  position: absolute;
  left: 0;
  bottom: 0;
  height: 60rpx;
  line-height: 60rpx;
  text-align: center;
  font-size: 32rpx;
  background-color: white;
  color: red;
}
.numActive {
  transform: translateX(0%);
  opacity: 1;
  visibility: visible;
}
.increaseActive {
  transform: rotateZ(-90deg);
}
.foodTitle {
  font-size: 35rpx;
}
.foodDetail {
  color: rgba(0, 0, 0, 0.7);
  margin-top: 10rpx;
  font-size: 25rpx;
}
</style>
